import {
    Tray,
    Menu,
    app,
    shell,
    nativeImage
  } from "electron";
  const path = require("path");
  
  export default class HTray {
    constructor() {
      this.tray = null;
      this.timer = null;
      this.url = "";
  
      if (process.platform === "win32") {
        this.normalImage = nativeImage.createFromPath('src/assets/img/logo.ico');
      } else if (process.platform === "darwin") {
        this.normalImage = nativeImage.createFromPath('src/assets/img/logo.png');
      } else {
        this.normalImage = nativeImage.createFromPath('src/assets/img/logo.png');
      }
  
      this.maskImage = nativeImage.createFromPath('src/assets/img/empty.png');
    }
  
    createMenu() {
      const self = this;
      // if (process.platform === "win32") {
      if (process.platform !== "darwin") {
        const trayMenuTemplate = [
          {
            label: "打开",
            click: function() {
              self.openWindow();
            }
          },
          {
            label: "进入官网",
            click: function() {
              self.openUrl();
            }
          },
          {
            label: "注销",
            click: function() {
              self.logout();
            }
          },
          {
            label: "退出",
            click: function() {
              self.quit();
            }
          }
        ];
        // TODO:: 图片路径需要区分devel pro
        // 当前目录下的app.ico图标
        // 图标的上下文菜单
        const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
        // 设置托盘悬浮提示
        this.tray.setToolTip("前端大狗");
        // 设置托盘菜单
        this.tray.setContextMenu(contextMenu);
      }
    }
  
    // 单击托盘小图标显示应用
    show() {
      this.tray = new Tray(this.normalImage);
      // this.tray = new Tray("../htchat/src/assets/img/icons/tray.png");
      this.createMenu();
      this.tray.on("click", () => {
        this.stopFlash();
        this.openWindow();
      });
      this.tray.on("show", () => {
        this.tray.setHighlightMode("always");
      });
      this.tray.on("hide", () => {
        this.tray.setHighlightMode("never");
      });
      // this.messageTwinkle()
    }
  
    openUrl() {
        console.log("guanwang is open")
    }
  
    openWindow() {
        console.log("win is open")
        app.mainWindow.win.isVisible() ? app.mainWindow.hide() : app.mainWindow.show();
    }
  
    // 图标闪烁
    startFlash() {
      if (global.platform === "mac" || this.timer) {
        return;
      }
  
      // 托盘闪烁
      let count = 0;
      this.timer = setInterval(() => {
        try {
          count++;
          if (count % 2 === 0) {
            this.tray.setImage(this.normalImage);
          } else {
            this.tray.setImage(this.maskImage);
          }
          if (count === 100) {
            count = 0;
          }
        } catch (err) {
          console.log(err);
          this.stopFlash();
        }
      }, 700);
  
      // if (process.platform === "win32") {
      if (process.platform !== "darwin") {
        if (app.mainWindown) {
          app.mainWindow.win.flashFrame(true); // 任务栏闪烁
        }
      }
    }
  
    stopFlash() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
      this.tray.setImage(this.normalImage);
  
      // if (process.platform === "win32") {
      if (process.platform !== "darwin") {
        if (app.mainWindow && global.token) {
          app.mainWindow.win.flashFrame(false);
        }
      }
    }
  
    // 退出
    logout() {
        console.log("win is logout")
    }
  
    // 注销
    async quit() {
        console.log("win is quit")
    }
  
    // 销毁
    destroy() {
      this.tray.destroy();
    }
  }
  